home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / clx.lha / clx / kcltcp.c < prev    next >
C/C++ Source or Header  |  1988-09-12  |  2KB  |  95 lines

  1. /* 
  2.  * stream interface to tcp for kcl for 4.3BSD
  3.  *
  4.  * Copyright (C) 1987 Massachussetts Institute of Technology 
  5.  *
  6.  * Roman Budzianowski - Project Athena/MIT
  7.  *
  8.  */
  9.  
  10. /* compile command:
  11.  * 
  12.  *     cc -c kcltcp.c -DVAX -DMAXPAGE=16384 -DVSSIZE=8152 -I${KCLHDIR}
  13.  *
  14.  *      where KCLHDIR is the h subdirectory in the kcl distribution
  15.  *
  16.  */
  17.  
  18. #include "include.h"
  19.  
  20. object
  21. open_tcp_stream(host,port)
  22.      object host;        /* host name */
  23.      object port;        /* port number */
  24. {
  25.    object streamTcp;
  26.    int fd;            /* file descriptor */
  27.    int i;
  28.    char hname[BUFSIZ];
  29.    int portnumber;
  30.    FILE *fout, *fin;
  31.    object streamIn, streamOut, make_stream();
  32.  
  33.    if (type_of(host) != t_string)
  34.      FEerror("~S is wrong type for host (should be string).",1,host);
  35.  
  36.    if(type_of(port) != t_fixnum)
  37.      FEerror("~S is wrong type for port (should be integer).",1,port);
  38.  
  39.    if (host->st.st_fillp > BUFSIZ - 1)
  40.      too_long_file_name(host);
  41.    for (i = 0;  i < host->st.st_fillp;  i++)
  42.      hname[i] = host->st.st_self[i];
  43.    hname[i] = '\0';
  44.  
  45.    portnumber = (int) fix(port);
  46.  
  47.    fd = connect_to_server(hname,portnumber); 
  48.  
  49.    if(fd < 0)
  50.      return Cnil;
  51.  
  52.    streamIn = make_stream(host,fd,smm_input);
  53.    streamOut = make_stream(host,fd,smm_output);
  54.  
  55.    streamTcp = make_two_way_stream(streamIn,streamOut);
  56.  
  57.    return(streamTcp);
  58. }
  59.  
  60. object make_stream(host,fd,smm)
  61.      object host;        /* not really used */
  62.      int fd;            /* file descriptor */
  63.      enum smmode smm;        /* lisp mode */
  64. {
  65.    object stream;
  66.    char *mode;            /* file open mode */
  67.    FILE *fp;            /* file pointer */
  68.    vs_mark;
  69.  
  70.    switch(smm){
  71.     case smm_input:
  72.       mode = "r";
  73.       break;
  74.     case smm_output:
  75.       mode = "w";
  76.       break;
  77.     default:
  78.       FEerror("make_stream : wrong mode");
  79.    }
  80.    
  81.    fp = fdopen(fd,mode);
  82.  
  83.    stream = alloc_object(t_stream);
  84.    stream->sm.sm_mode = (short)smm;
  85.    stream->sm.sm_fp = fp;
  86.    fp->_base = BASEFF; 
  87.    stream->sm.sm_object0 = Sstring_char;
  88.    stream->sm.sm_object1 = host;
  89.    stream->sm.sm_int0 = stream->sm.sm_int1 = 0;
  90.    vs_push(stream);
  91.    setbuf(fp, alloc_contblock(BUFSIZ)); 
  92.    vs_reset;
  93.    return(stream);
  94. }
  95.